package com.ruoyi.wq.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.Arith;
import com.ruoyi.common.utils.Condition;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqUserVisit;
import com.ruoyi.wq.domain.vo.WqUserVisitVO;
import com.ruoyi.wq.mapper.WqUserVisitMapper;
import com.ruoyi.wq.service.IWqUserVisitService;

/**
 * 用户访客记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-26
 */
@Service
public class WqUserVisitServiceImpl extends ServiceImpl<WqUserVisitMapper, WqUserVisit> implements IWqUserVisitService {
    
	@Autowired
    private WqUserVisitMapper wqUserVisitMapper;
	
	@Autowired
	private ISysUserService sysUserService;

    /**
     * 查询用户访客记录
     * 
     * @param id 用户访客记录主键
     * @return 用户访客记录
     */
    @Override
    public WqUserVisit selectWqUserVisitById(String id)
    {
        return wqUserVisitMapper.selectWqUserVisitById(id);
    }

    /**
     * 查询用户访客记录列表
     * 
     * @param wqUserVisit 用户访客记录
     * @return 用户访客记录
     */
    @Override
    public List<WqUserVisit> selectWqUserVisitList(WqUserVisit wqUserVisit)
    {
        return wqUserVisitMapper.selectWqUserVisitList(wqUserVisit);
    }

    /**
     * 新增用户访客记录
     * 
     * @param wqUserVisit 用户访客记录
     * @return 结果
     */
    @Override
    public int insertWqUserVisit(WqUserVisit wqUserVisit)
    {
        wqUserVisit.setCreateTime(DateUtils.getNowDate());
        return wqUserVisitMapper.insertWqUserVisit(wqUserVisit);
    }

    /**
     * 修改用户访客记录
     * 
     * @param wqUserVisit 用户访客记录
     * @return 结果
     */
    @Override
    public int updateWqUserVisit(WqUserVisit wqUserVisit)
    {
        wqUserVisit.setUpdateTime(DateUtils.getNowDate());
        return wqUserVisitMapper.updateWqUserVisit(wqUserVisit);
    }

    /**
     * 批量删除用户访客记录
     * 
     * @param ids 需要删除的用户访客记录主键
     * @return 结果
     */
    @Override
    public int deleteWqUserVisitByIds(String[] ids)
    {
        return wqUserVisitMapper.deleteWqUserVisitByIds(ids);
    }

    /**
     * 删除用户访客记录信息
     * 
     * @param id 用户访客记录主键
     * @return 结果
     */
    @Override
    public int deleteWqUserVisitById(String id)
    {
        return wqUserVisitMapper.deleteWqUserVisitById(id);
    }

	@Override
	public TableDataInfo getList(WqUserVisitVO wqUserVisit) {
		SysUser user = sysUserService.getById(SecurityUtils.getUserId());
		user.setVisitRead("0");
		sysUserService.updateById(user);
		
		wqUserVisit.setLat(user.getLat());
		wqUserVisit.setLon(user.getLon());
		wqUserVisit.setUserId(SecurityUtils.getUserId());
		IPage<WqUserVisitVO> page = baseMapper.selectPageList(Condition.getPage(wqUserVisit), wqUserVisit);
		for(WqUserVisitVO vo : page.getRecords()){
    		vo.setDistance(String.valueOf(Arith.div(vo.getMi(), 1000.00, 2)));
    		vo.setTime(DateUtils.getTime(vo.getUpdateTime()));
        }
		return Condition.getTableDataInfo(page.getRecords(), page.getTotal());
	}

	@Override
	public Integer addVisit(Long userId) {
		Long visitUserId = SecurityUtils.getUserId();
		SysUser sysUser = sysUserService.getById(visitUserId);
		
		QueryWrapper<WqUserVisit> queryWrapper = new QueryWrapper<>();
		WqUserVisit entity = new WqUserVisit();
		entity.setVisitUserId(visitUserId);
		entity.setUserId(userId);
		queryWrapper.setEntity(entity);
		WqUserVisit visit = baseMapper.selectOne(queryWrapper);
		if(visit == null){
			visit = new WqUserVisit();
			visit.setVisitUserId(sysUser.getUserId());
			visit.setVisitSex(sysUser.getSex());
			visit.setVisitAvatar(sysUser.getAvatar());
			visit.setVisitNickName(sysUser.getNickName());
			visit.setVisitAge(sysUser.getAge());
			visit.setCity(sysUser.getCity());
			visit.setLon(sysUser.getLon());
			visit.setLat(sysUser.getLat());
			visit.setUserId(userId);
			baseMapper.insert(visit);
		}else{
			Integer visitNum = visit.getVisitNum();
			visit.setVisitSex(sysUser.getSex());
			visit.setVisitAvatar(sysUser.getAvatar());
			visit.setVisitNickName(sysUser.getNickName());
			visit.setVisitAge(sysUser.getAge());
			visit.setCity(sysUser.getCity());
			visit.setLon(sysUser.getLon());
			visit.setLat(sysUser.getLat());
			visit.setVisitNum(visitNum+1);
			baseMapper.updateById(visit);
		}
		
		
		QueryWrapper<WqUserVisit> qw = new QueryWrapper<>();
		WqUserVisit uv = new WqUserVisit();
		uv.setUserId(userId);
		qw.setEntity(uv);
		return baseMapper.selectCount(qw);
	}
}
